package pl.mw.cmd.internal.telnetserver;

import static org.jboss.netty.channel.Channels.pipeline;

import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.execution.ExecutionHandler;

/**
 * Tworzy odpowiedni {@link ChannelPipeline} dla nowego kanalu.
 */
public class TelnetServerPipelineFactory implements ChannelPipelineFactory {

	/** Handler polaczen. */
	private ChannelHandler handler;
	
	/** Handler wykonania. */
	private ChannelHandler executionHandler;
	
	/**
	 * @param pOpenChannels otwarte kanaly. 
	 * @param handler polaczen
	 * @param handler wykonania
	 */
	public TelnetServerPipelineFactory(ChannelHandler handler, ExecutionHandler executionHandler) {
		this.handler = handler;
		this.executionHandler = executionHandler;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ChannelPipeline getPipeline() throws Exception {
		
		ChannelPipeline pipeline = pipeline();

		pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
		pipeline.addLast("decoder", new StringDecoder());
		pipeline.addLast("encoder", new StringEncoder());
		pipeline.addLast("executionHandler", this.executionHandler);
		pipeline.addLast("handler", this.handler);

		return pipeline;
	}
}
